<template>
	<view class="content">
		<view class="text-area">
			<text class="title">{{title}}</text>
			<text class="title">{{user.username}}</text>
			<text class="title">{{user.user_id}}</text>
		</view>
		
		<view class="text-area">
			<button class="title" @click="post">post</button>
		</view>
		</br>
		<view class="text-area">
			<button class="title" @click="get">get</button>
		</view>
		</br>
		
		<form @submit="regSubmit">
			<view class="">
				<input class="uni-input" name="phone" placeholder="phone" />
			</view>
			<view class="">
				<input class="uni-input" name="password" placeholder="password" />
			</view>
			
			<button form-type="submit">reg</button>
		</form>
		
		<form @submit="loginSubmit">
			<view class="">
				<input class="uni-input" name="phone" placeholder="phone" />
			</view>
			<view class="">
				<input class="uni-input" name="password" placeholder="password" />
			</view>
			<button form-type="submit">login</button>
		</form>
		
		<form @submit="editSubmit">
			<view class="">
				<input class="uni-input" name="phone" placeholder="phone" />
			</view>
			<button form-type="submit">edit</button>
		</form>
		<view class="text-area">
			<button class="title" @click="del">del</button>
		</view>
		<form @submit="logoutSubmit">
			<button form-type="submit">logout</button>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'laravel',
				user: {},
			}
		},
		onLoad() {

		},
		methods: {
			post(){
				this.api.request({url:'/api/v1/user',method:'POST',params:{page:2,num:10},
				}).then(res=>{
					console.log(res);
				});
			},
			get(){
				this.api.request({url:'/api/v1/user',method:'GET',params:{page:2,num:10},
				}).then(res=>{
					console.log(res);
				});
			},
			regSubmit: function(e) {
				var formdata = e.detail.value
				this.api.request({url:'/api/v1/user/register',method:'POST',params:formdata,
				}).then(res=>{
					if(res.code==422){
						console.log(res);
						var msg = '';
						for(var index in res.data){
							msg = msg+res.data[index]+' ';
						}
						uni.showToast({
							title:msg,
							duration: 4000,
							icon: "none"
						})
					}else if(res.code==621){
						this.user = this.api.login(res.data);
					}
				});
			},
			loginSubmit: function(e) {
				var formdata = e.detail.value;
				this.api.request({url:'/api/v1/user/login',method:'POST',params:formdata,
				}).then(res=>{
					//console.log(res);
					if(res.code==631){
						this.user = this.api.login(res.data);
					}
				});
			},
			del(){
				this.api.request({url:'/api/v1/user/del',method:'GET',params:{id:5},
				}).then(res=>{
					console.log(res);
				});
			},
			logoutSubmit(){
				this.api.request({url:'/api/v1/user/logout',method:'GET'
				}).then(res=>{
					//console.log(res);
					if(res.code==641){
						this.user = this.api.logout();
					}
				});
			},
		}
	}
</script>

